package com.quyunshuo.module.home.data

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import com.quyunshuo.module.home.R

class DataAdapter(private val dataList: List<DataItem>) :
    RecyclerView.Adapter<DataAdapter.DataViewHolder>() {

    inner class DataViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val tvNumber: TextView = itemView.findViewById(R.id.tvNumber)
        val tvTitle: TextView = itemView.findViewById(R.id.tvTitle)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DataViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_data, parent, false)
        return DataViewHolder(view)
    }

    override fun onBindViewHolder(holder: DataViewHolder, position: Int) {
        val item = dataList[position]
        holder.tvNumber.text = "#${item.id}"
        holder.tvTitle.text = item.title
        
        // 添加点击事件
        holder.itemView.setOnClickListener {
            Toast.makeText(
                holder.itemView.context,
                item.description,
                Toast.LENGTH_SHORT
            ).show()
        }
    }

    override fun getItemCount(): Int = dataList.size
}